#include "bookmarkdialog.h"
#include <QtGui>
BookMarkDialog::BookMarkDialog(QWidget *parent) :
    QDialog(parent)
{
	lvBookMark=new QListView(this);
	lvBookMark->setModel(&model);
	connect(lvBookMark,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(onok()));
	pbdel=new QPushButton(tr("Delete"),this);
	pbOK=new QPushButton(tr("OK"),this);
	pbOK->setEnabled(0);
	pbCancel=new QPushButton(tr("Cancel"),this);
	connect(pbOK,SIGNAL(clicked()),this,SLOT(onok()));
	connect(pbdel,SIGNAL(clicked()),this,SLOT(ondel()));
	connect(lvBookMark,SIGNAL(clicked(QModelIndex)),this,SLOT(onchange()));
	connect(pbCancel,SIGNAL(clicked()),this,SLOT(oncancel()));
	QHBoxLayout *hl=new QHBoxLayout(this);
	QVBoxLayout *vl=new QVBoxLayout(this);
	vl->addWidget(pbOK);
	vl->addWidget(pbdel);
	vl->addWidget(pbCancel);
	vl->addStretch();
	hl->addWidget(lvBookMark);
	hl->addLayout(vl);
	setLayout(hl);
}
void BookMarkDialog::ondel()
{
	QModelIndexList l=lvBookMark->selectionModel()->selectedRows();
	if(!l.isEmpty())
	{
		model.removeRow(l.first().row());
	}
	*bookMarks=model.stringList();
}
void BookMarkDialog::onok()
{
	pbOK->setEnabled(0);
	QModelIndexList l=lvBookMark->selectionModel()->selectedRows();
	if(!l.isEmpty())
	{
		emit open(bookMarks->at(l.first().row()));
	}
	close();
}
void BookMarkDialog::setBookMarks(QStringList *s)
{
	bookMarks=s;
	model.setStringList(*s);
}
void BookMarkDialog::onchange()
{
	//qDebug()<<"changed";
	QModelIndexList l=lvBookMark->selectionModel()->selectedRows();
	if(!l.isEmpty())
		pbOK->setEnabled(1);
}
void BookMarkDialog::upDateBookMarks()
{
	model.setStringList(*bookMarks);
}
void BookMarkDialog::oncancel()
{
	pbOK->setEnabled(0);
	close();
}
